//
//  XCInTheSongListViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/15.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCInTheSongListViewController.h"
#import "XCShareMusicTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "XCInTheSongListModel.h"
#import "OnlineAVplay.h"
#import "XCUrlManger.h"
#import "XCAFNetTool.h"
#import "XCInTheSongListModel.h"
#import "XCPlayViewController.h"
@interface XCInTheSongListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSString *picUrl;

@property (nonatomic, retain) NSNumber *quan_id;

@property (nonatomic, retain) UIImageView *inTheSongListImageView;

@property (nonatomic, copy) NSString *navigationName;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;


@end

@implementation XCInTheSongListViewController

- (void)dealloc {
    
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_picUrl release];
    [_dataSource release];
    [_quan_id release];
    [_inTheSongListImageView release];
    [_tableView release];
    [_mutableArraySingerNAme release];
    [_mutableArraySongName release];
    [_nameArray release];
    [_mutableHeadIVPicurl release];
    
    [super dealloc];
}

- (instancetype)initWithQuan_id:(NSNumber *)quan_id withTitle:(NSString *) title
{
    self = [super init];
    if (self) {
        self.quan_id = quan_id;
        self.navigationName = title;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.navigationName;
    
    [self inTheSongListNetRequest];
    [self inTheSongListTableViewBuild];
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)inTheSongListNetRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger inTheSongListNetRequest:self.quan_id] body:nil headerFile:nil response:XCJSON success:^(id result) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"songs"]) {
            XCInTheSongListModel *inTheSongListModel = [[XCInTheSongListModel alloc] initWithDataSource:dic];
            if (inTheSongListModel.urlList.count != 0 && inTheSongListModel.picUrl != nil && inTheSongListModel.singerName != nil && [inTheSongListModel.urlList[0] objectForKey:@"url"] != nil) {
                [self.dataSource addObject:inTheSongListModel];
            }
            [inTheSongListModel release];
        }
        
        
        XCInTheSongListModel *inTheSongListModel = self.dataSource[arc4random() % 10 + 1];
        [self.inTheSongListImageView sd_setImageWithURL:[NSURL URLWithString:inTheSongListModel.picUrl]placeholderImage:[UIImage imageNamed:@"Image-p"]];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
}
- (void)inTheSongListTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.inTheSongListImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    [view addSubview:self.inTheSongListImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [view release];
    [_tableView release];
    [_inTheSongListImageView release];
}

#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    XCInTheSongListModel *inTheSongListModel = self.dataSource[indexPath.row];
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.inTheSongListModel = inTheSongListModel;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    for (XCInTheSongListModel *inTheSongListModel in self.dataSource) {
        if (inTheSongListModel.urlList.count != 0 && inTheSongListModel.picUrl != nil && inTheSongListModel.singerName != nil && [inTheSongListModel.urlList[0] objectForKey:@"url"] != nil) {
            [self.mutableHeadIVPicurl addObject:inTheSongListModel.picUrl];
            [self.mutableArraySingerNAme addObject:inTheSongListModel.singerName];
            [self.nameArray addObject:inTheSongListModel.name];
            [self.mutableArraySongName addObject:[inTheSongListModel.urlList[0] objectForKey:@"url"]];
        }
    }
    
    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    
    XCInTheSongListModel *inTheSongListModel1 = self.dataSource[indexPath.row];
    [[OnlineAVplay shareAVPlayer] playWithUrl:[inTheSongListModel1.urlList[0] objectForKey:@"url"]];
    [[OnlineAVplay shareAVPlayer] play];
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.inTheSongListImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.inTheSongListImageView.transform = CGAffineTransformMakeScale(scale, scale);

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 3;
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
